import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAddonDevelopService extends BaseService<any> {
  private readonly logger = new Logger(CoreAddonDevelopService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * add
   * 对应 PHP: CoreAddonDevelopService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: if (is_dir($this->base_addon_dir)) throw new AddonException('ADDON_KEY_IS_EXIST');//当前目录中已存在key值一致的插件         $this->setAddonInfo($data);          $th...
if (is_dir(this.base_addon_dir)) throw new AddonBusinessException('ADDON_KEY_IS_EXIST'];//当前目录中已存在key值一致的插件
        this.setAddonInfo(data];

        this.filePut(this.map, this.base_addon_dir);


        return true;
    }

    async setAddonInfo(data)
    {
        data.key = this.key;
        this.addon_info = data;
        this.addon_info.support_version = config('version.version');
    }

    /**
     * 文件创建
     * @param item
     * @param root_k
     * @param key
     * @return true
     */
    async filePut(item, root_k = '', key = '')
    {
        //key为int为文件,否者是文件夹
        if (is_int(key)) {
            this.fileAdd(item, root_k);
  }

  /**
   * setAddonInfo
   * 对应 PHP: CoreAddonDevelopService_core::setAddonInfo()
   * 逻辑类型: undefined - undefined
   */
  async setAddonInfo(data: any) {
    // 基于PHP真实逻辑: setAddonInfo
    // PHP原文: $data['key'] = $this->key;         $this->addon_info = $data;         $this->addon_info['support_version'] = config('version.version');     }      /**...
data.key = this.key;
        this.addon_info = data;
        this.addon_info.support_version = config('version.version');
    }

    /**
     * 文件创建
     * @param item
     * @param root_k
     * @param key
     * @return true
     */
    async filePut(item, root_k = '', key = '')
    {
        //key为int为文件,否者是文件夹
        if (is_int(key)) {
            this.fileAdd(item, root_k);
  }

  /**
   * filePut
   * 对应 PHP: CoreAddonDevelopService_core::filePut()
   * 逻辑类型: undefined - undefined
   */
  async filePut(item: any, root_k: any, key: any) {
    // 基于PHP真实逻辑: filePut
    // PHP原文: //key为int为文件,否者是文件夹         if (is_int($key)) {             $this->fileAdd($item, $root_k);...
//key为int为文件,否者是文件夹
        if (is_int(key)) {
            this.fileAdd(item, root_k);
  }

  /**
   * fileAdd
   * 对应 PHP: CoreAddonDevelopService_core::fileAdd()
   * 逻辑类型: undefined - undefined
   */
  async fileAdd(item: any, dir: any) {
    // 基于PHP真实逻辑: fileAdd
    // PHP原文: $is_cover = $item['is_cover'] ?? false;         if ($this->action == 'edit' && !$is_cover) {             return true;...
const is_cover = item.is_cover || false;
        if (this.action == 'edit' && !is_cover) {
            return true;
  }

  /**
   * contentReplace
   * 对应 PHP: CoreAddonDevelopService_core::contentReplace()
   * 逻辑类型: undefined - undefined
   */
  async contentReplace(content: any, vars: any) {
    // 基于PHP真实逻辑: contentReplace
    // PHP原文: foreach ($vars as $k => $v) {             $content = str_replace('{' . $k . '}', $v, $content);...
foreach (vars as k => v) {
            const content = str_replace('{' . k  + '}', v, content);
  }

  /**
   * edit
   * 对应 PHP: CoreAddonDevelopService_core::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: if (!is_dir($this->base_addon_dir)) throw new AddonException('ADDON_IS_NOT_EXIST');//当前目录中不存在此项插件         $this->action = 'edit';         $this->setAd...
if (!is_dir(this.base_addon_dir)) throw new AddonBusinessException('ADDON_IS_NOT_EXIST'];//当前目录中不存在此项插件
        this.action = 'edit';
        this.setAddonInfo(data];

        this.filePut(this.map, this.base_addon_dir];
        //如果已安装的插件,需要同步修改表记录
        const where = [
            [
                'key', '=', this.key
            ]
        );
        const info = this.model.where(where).findOrEmpty(];
        if (!info.isEmpty()) {
            info.save(
                [
                    title: data.title,
                    desc: data.desc,
                    author: data.author,
                    version: data.version,
                    type: data.type,
                    support_app: data.support_app,
                    update_time: time(),
                ]
            ];
  }

  /**
   * del
   * 对应 PHP: CoreAddonDevelopService_core::del()
   * 逻辑类型: undefined - undefined
   */
  async del() {
    // 基于PHP真实逻辑: del
    // PHP原文: if (!is_dir($this->base_addon_dir)) throw new AddonException('ADDON_IS_NOT_EXIST');//当前目录中不存在此项插件         $where = [             [                 'ke...
if (!is_dir(this.base_addon_dir)) throw new AddonBusinessException('ADDON_IS_NOT_EXIST'];//当前目录中不存在此项插件
        const where = [
            [
                'key', '=', this.key
            ]
        );
        const info = this.model.where(where).findOrEmpty(];
        if (!info.isEmpty()) {
            throw new AddonBusinessException('ADDON_IS_INSTALLED_NOT_ALLOW_DEL');
  }
}
